<?php
namespace App\Controllers\Collect;
use App\Models\Collect\Collect;
use  App\Models\Goods\Goods;
/**
*	设置收藏信息
*/
class SetCollect extends \App\Libs\Base\BaseAction
{
	protected function beforeRun() {
         parent::beforeRun();
         $this->needLogin = TRUE;
    }
	
	public function execute() {
		//获取类型
		$type = $this->request->get( 'type' );
		if( empty( $type ) ){
			return \App\Libs\Views\Json::view(1, '缺少相关参数', '');	
		}
		$cid = $this->request->get( 'cid' );
		switch( $type ){
			case 1:
				//商品
				$goods = Goods::find(array(
					' id = :gid: ',
					'bind' => array( 'gid' => $cid )
				));
				if( empty($goods) ){
					return \App\Libs\Views\Json::view(1, '收藏商品不存在', null);		
				}
				break;
			case 2:
				break;
			case 3:
				//体验商品
				$goods = Goods::find(array(
					' id = :gid: and is_experience = 1',
					'bind' => array( 'gid' => $cid )
				));
				if( empty($goods) ){
					return \App\Libs\Views\Json::view(1, '收藏商品不存在', null);		
				}
				break;
			default:
					return \App\Libs\Views\Json::view(1, '参数有误', null);	
				break;
		}
		$collect = new Collect();
		$collect->uid = $this->session->user->id;	
		$collect->cid = $cid;	
		$collect->type = $type;	
		$collect->ctime = date("Y-m-d H:i:s");	
		$info = $collect->save();
		//$info = Collect::setCollectInfo( $this->session->user->id , $cid , $type );
		
		//系统错误
		if (!$info) {
			return \App\Libs\Views\Json::view( 1 , '收藏失败', null);
		}
		//返回成功数据
		return \App\Libs\Views\Json::view( 0 , '成功', null);
	}
}
